package me.lovetcat.common.dto;

import me.lovetcat.common.constant.Resp;

import java.util.List;

/**
 * @author acat on 2017-01-10 16:21
 */
public class RespDto<T> {

    private String result;

    private Integer code;

    private String msg;

    private T data;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public RespDto() {
    }

    public RespDto(String result, Integer code, String msg, T data) {
        this.result = result;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public RespDto(String result, Resp.Code code, T data) {
        this.result = result;
        this.data = data;
        this.code = code.value();
        this.msg = code.msg();
    }

    public static RespDto ok(){
        return new RespDto(Resp.Result.OK, Resp.Code.OK, null);
    }

    public static RespDto ok(String msg){
        return new RespDto(Resp.Result.OK, Resp.Code.OK.value(), msg, null);
    }

    public static <T> RespDto ok(T data){
        return new RespDto(Resp.Result.OK, Resp.Code.OK, data);
    }

    public static <T> RespDto ok(String msg, T data){
        return new RespDto(Resp.Result.OK, Resp.Code.OK.value(), msg, data);
    }

    public static RespDto fail(int code, String msg){
        return new RespDto(Resp.Result.FAIL, code, msg, null);
    }

    public static RespDto fail_404(){
        return new RespDto(Resp.Result.FAIL, Resp.Code.FAIL_404, null);
    }

    public static RespDto fail_valid(List<FieldErrorDto> fieldErrorDtoList){
        return new RespDto(Resp.Result.FAIL, Resp.Code.FAIL_PARAMS_VALID, fieldErrorDtoList);
    }

    @Override
    public String toString() {
        return "RespObj{" +
                "result='" + result + '\'' +
                ", code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
